package com.icetec.iceintel.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import com.icetec.R;
import com.icetec.iceintel.database.DevicesListSqlHelper;
import com.icetec.iceintel.framework.AppApplication;
import com.icetec.iceintel.model.Device;
import com.icetec.iceintel.netrequest.UniResAllocator;

/**
 * Created by amo on 2015/3/13.
 * 选择单个灯具的界面
 */
public class SelectDeviceGridViewAdapter extends BaseAdapter {

    private ArrayList<Device> deviceArrayList = new ArrayList<Device>();
    LayoutInflater layoutInflater;
    DevicesListSqlHelper devicesListSqlHelper;
    Animation animationIn, animationOut;
    int arraySize;

    Device selectedDevice;


    public SelectDeviceGridViewAdapter() {


        devicesListSqlHelper = new DevicesListSqlHelper();
        this.deviceArrayList.addAll(devicesListSqlHelper.getAllDeviceEceeptGroup());
        layoutInflater = LayoutInflater.from(AppApplication.getShareInstance());
        animationIn = new AlphaAnimation(0.0f, 1.0f);
        animationIn.setDuration(200);
        animationOut = new AlphaAnimation(1.0f, 0f);
        animationOut.setDuration(200);


    }


    public void updateListViewFromDb() {
        this.deviceArrayList.addAll(devicesListSqlHelper.getAllDevice());
        //从数据库中更新 deviceArrayList
        notifyDataSetChanged();


    }

   public void setAllDevicesUncheck(){
       for(Device device:deviceArrayList){

           device.setExtra("no");
       }
       notifyDataSetChanged();
   }


    @Override
    public int getCount() {
        arraySize = deviceArrayList.size();
        return arraySize;
    }

    @Override
    public Object getItem(int position) {
        return deviceArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final LightListHolder lightListHolder;
        final Device device = (Device) getItem(position);

        if (convertView == null) {

            convertView = (View) layoutInflater.inflate(R.layout.component_select_single_device, null);
            lightListHolder = new LightListHolder();
            lightListHolder.lightName = (TextView) convertView.findViewById(R.id.lightlist_item_group_tv);
            lightListHolder.lightIcon = (ImageView) convertView.findViewById(R.id.scheme_devicelist_icon_iv);
            lightListHolder.lightSelector = (CheckBox) convertView.findViewById(R.id.lightlist_item_group_checkbox);
            lightListHolder.itemLayout = (RelativeLayout) convertView.findViewById(R.id.lightlist_item_group_rely);

            convertView.setTag(lightListHolder);
        } else {
            lightListHolder = (LightListHolder) convertView.getTag();
        }

        lightListHolder.lightSelector.setClickable(false);


        if (device.getExtra() == null || device.getExtra().equals("no")) {

            lightListHolder.lightSelector.setChecked(false);


        } else {

            lightListHolder.lightSelector.setChecked(true);

        }


        lightListHolder.itemLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    setAllDevicesUncheck();
                    device.setExtra("yes");
                    selectedDevice=device;
                    lightListHolder.lightSelector.setChecked(true);

            }
        });

        if (device.getOnoff() != null && device.getOnoff().equals("on")) {

            lightListHolder.lightIcon.setImageDrawable(UniResAllocator.getResIDbyType(device.getType(), true));

        } else {

            lightListHolder.lightIcon.setImageDrawable(UniResAllocator.getResIDbyType(device.getType(), false));
        }

        if (device.getName() != null) {
            lightListHolder.lightName.setText(device.getName());

        } else {
            lightListHolder.lightName.setText(R.string.unnamed_light);
        }

        return convertView;
    }


    public class LightListHolder {
        ImageView lightIcon;
        CheckBox lightSelector;
        TextView lightName;
        RelativeLayout itemLayout;
    }


    public Device getSelectedDevice() {
        return selectedDevice;
    }
}